package com.sskj.xgja.mine;


import android.content.Context;
import android.content.Intent;
import android.text.InputFilter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.hey.lib.HeySpinner;
import com.hjq.toast.ToastUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.base.data.User;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.utils.CopyUtil;
import com.sskj.base.utils.MoneyValueFilter;
import com.sskj.base.utils.TipDialog;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xgja.MVPActivity;
import com.sskj.xgja.R;
import com.sskj.xgja.WebViewActivity;
import com.sskj.xgja.api.SystemInfo;
import com.sskj.xgja.mine.data.BankBean;
import com.sskj.xgja.mine.data.RechargeBean;
import com.sskj.xgja.mine.data.ShopInfo;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Flowable;

/**
 * 入金--充值
 *
 * @author Hey
 * Create at  2019/03/27
 */
public class RechargeActivity extends MVPActivity<RechargePresenter> {

    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;
    @BindView(R.id.availableText)
    TextView availableText;
    @BindView(R.id.shop_info)
    TextView shopInfo;
    @BindView(R.id.bank_spinner)
    HeySpinner bankSpinner;
    @BindView(R.id.edt_price)
    EditText edtPrice;
    @BindView(R.id.totalPriceText)
    TextView totalPriceText;
    @BindView(R.id.submit)
    Button submit;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.tv1)
    TextView tv1;
    @BindView(R.id.tv1_copy)
    TextView tv1Copy;
    @BindView(R.id.tv2)
    TextView tv2;
    @BindView(R.id.tv2_copy)
    TextView tv2Copy;
    @BindView(R.id.tv3)
    TextView tv3;
    @BindView(R.id.tv3_copy)
    TextView tv3Copy;
    private double usdRate;
    private List<String> bankNames = new ArrayList<>();
    private List<BankBean> bankData = new ArrayList<>();
    private String selectBankCode;
    private double minRecharge;

    private boolean bankLoad = true, rateLoad;
    private TipDialog tipDialog;
    private ShopInfo dataInfo;
    private List<RechargeBean> payData;

    @Override
    public int getLayoutId() {
        return R.layout.activity_recharge;
    }

    @Override
    public RechargePresenter getPresenter() {
        return new RechargePresenter();
    }

    @Override
    public void initView() {
//        bankSpinner.setOnSelectListener(position -> {
//            selectBankCode = bankData.get(position).getBankCardTypeCode();
//        });
        toolbar.setLeftButtonOnClickListener(v -> finish());
        ClickUtil.click(tv1Copy, view -> {
            if (dataInfo != null) {
                CopyUtil.copy(this, dataInfo.getName());
                ToastUtils.show("户名复制成功");
            }
        });
        ClickUtil.click(tv2Copy, view -> {
            if (dataInfo != null) {
                CopyUtil.copy(this, dataInfo.getAccount());
                ToastUtils.show("账号复制成功");
            }
        });
        ClickUtil.click(tv3Copy, view -> {
            if (dataInfo != null) {
                CopyUtil.copy(this, dataInfo.getAddr());
                ToastUtils.show("开户银行复制成功");
            }
        });
        ClickUtil.click(submit, view -> {
            tipDialog = new TipDialog(this)
                    .setTitle("提示")
                    .setContent("请确认入金")
                    .setListener(dialog -> {
                        if (isEmpty(edtPrice)) {
                            ToastUtils.show("请输入入金金额");
                            return;
                        }
                        if (payData==null){
                            mPresenter.getPayWayNew();
                            return;
                        }
                        mPresenter.xgRecharge(getText(edtPrice),payData.get(bankSpinner.getSelectIndex()).getP4_typeCode());
                    });
            tipDialog.show();
//            if (!bankLoad) {
//                ToastUtils.show("数据加载失败，请下拉刷新后重试");
//                return;
//            }


//            if (Double.parseDouble(getText(totalPriceText)) < minRecharge) {
//                ToastUtils.show("入金金额最小为$" + minRecharge);
//                return;
//            }

//            mPresenter.getPayWay(getText(edtPrice));


        });
        edtPrice.setFilters(new InputFilter[]{new MoneyValueFilter(2)});
//        edtPrice.addTextChangedListener(new SimpleTextWatcher() {
//            @Override
//            public void afterTextChanged(Editable s) {
//
//                if (!rateLoad) {
//                    ToastUtils.show("数据加载失败，请下拉刷新后重试");
//                    return;
//                }
//
//                if (!TextUtils.isEmpty(s)) {
//                    double cny = Double.parseDouble(s.toString());
//                    double udt = cny / usdRate;
//                    setText(totalPriceText, NumberUtil.keepHalfMax2(udt));
//                } else {
//                    setText(totalPriceText, "---");
//                }
//            }
//        });
    }

    @Override
    public void initData() {
        mPresenter.getSystemInfo();
        mPresenter.getShopInfo();
        mPresenter.getPayWayNew();
//        mPresenter.getBank(account);
        initRefreshLayout(smartRefreshLayout, false);
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
//            mPresenter.getBank(account);
            mPresenter.getSystemInfo();
        });

//        bankSpinner.setClickListener(() -> {
//            if (bankSpinner.getAdapter() == null) {
//                mPresenter.getBank(account);
//                return true;
//            }
//            return false;
//        });
    }

    public void stopRefresh() {
        smartRefreshLayout.finishRefresh();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mPresenter.getAccountDetail(account, true);
    }

    public static void start(Context context) {
        Intent intent = new Intent(context, RechargeActivity.class);
        context.startActivity(intent);
    }


    public void setBankData(List<BankBean> data) {
        if (data != null) {
            bankLoad = true;
            bankData = data;
            for (int i = 0; i < data.size(); i++) {
                bankNames.add(data.get(i).getBankCardType());
            }
            bankSpinner.attachData(bankNames);
        }
    }
    public void setPayData(List<RechargeBean> data){
        payData = data;
        Flowable.fromIterable(data)
                .map(rechargeBean -> rechargeBean.getP4_typeName())
                .toList()
                .subscribe((strings, throwable) -> {
                   bankSpinner.attachData(strings);
                });
    }

    public void setSysInfo(SystemInfo data) {
        rateLoad = true;
        usdRate = data.getInRate();
        minRecharge = data.getMinInCharge();
    }

    public void onPaySuccess(String data) {
        tipDialog.dismiss();
        WebViewActivity.start(this, data);
    }

    public void setAccountInfo(User data) {
        // 房 ---隐藏
        setText(availableText, data.getWallone());
    }

    public void updateShopInfo(ShopInfo data) {
        dataInfo = data;
        tv1.setText("户名:" + data.getName());
        tv2.setText("账号:" + data.getAccount());
        tv3.setText("开户银行:" + data.getAddr());
        shopInfo.setText("把转账截图和+登入账号+注册姓名发送公众号" + "\n" + "线下转账有疑问请咨询客服:QQ:" + data.getQq() + "或者" + "\n" + "关注:\"" + data.getGongzh() + "\"公众号");
    }


}
